
package ar.edu.unq.tpi.uis.tepe3.dominio;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/*
 * Existen carpetas, que forman una estructura arborea.
 * Hay una carpeta root, y debajo múltiples niveles de carpetas hijas.
 * El sistema debe permitir crear, borrar y editar carpetas.
 */

/**
 * @author gpilla <gpilla@unq.edu.ar>
 */
public class Folder implements Serializable{
	
	private static final long serialVersionUID = -390297281754672291L;
	
	private String name;	
	private Folder parent;
	private List<Folder> children;
	
	private List<Access> access;
	
	/**
	 * @param aName
	 */
	public Folder(String aName) {
		this.setName(aName);
		this.parent = null;
		this.setChildren(new ArrayList<Folder>());
		this.setAccesses(new ArrayList<Access>());
	}

	/**
	 * @param aName
	 * @param aParent
	 */
	public Folder(String aName, Folder aParent){
		this.setName(aName);
		this.setParent(aParent);
		this.setChildren(new ArrayList<Folder>());
		this.setAccesses(new ArrayList<Access>());
	}

	/**
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Setea el padre.
	 * 
	 * Y como plus... maneja el tema del doble conocimiento.
	 * 
	 * @param aFolder
	 */
	public void setParent(Folder aFolder) {
		if(this.parent != null){
			this.parent.removeChild(this);
		}
		this.parent = aFolder;
		aFolder.addChild(this);
	}

	/**
	 * 
	 * @return
	 */
	public Folder getParent() {
		return parent;
	}

	/**
	 * @param children
	 */
	protected void setChildren(List<Folder> children) {
		this.children = children;
	}

	/**
	 * @return
	 */
	public List<Folder> getChildren() {
		return children;
	}
	
	/**
	 * @param accesses
	 */
	protected void setAccesses(List <Access> accesses){
		this.access = accesses;
	}
	
	/**
	 * @return
	 */
	public List<Access> getAccesses(){
		return this.access;
	}
	
	public Integer getPrivilegesCount(){
		return this.getAccesses().size(); 
	}
	
	//---------------------------------------------------------
	// Metodos de acceso a datos
	//---------------------------------------------------------
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString (){
		return this.getPath();
	}
	
	/**
	 * Genera el path completo.
	 *
	 * @return
	 */
	public String getPath(){
		String path = "";
		Folder aFolder = this;
		while (!aFolder.isRoot()) {
			path = aFolder.getName() + "/" + path;
			aFolder = aFolder.getParent(); 			
		}
		return aFolder.getName() + "/" + path;
	}
	
	/**
	 * @return
	 */
	public Boolean isRoot(){
		return this.parent == null;
	}
	
	/**
	 * @return
	 */
	public Boolean isLeave(){
		return this.parent != null;
	}

	/**
	 * @param aChild
	 */
	public void addChild(Folder aChild){
		this.children.add(aChild);
	}
	
	/**
	 * @param aFolder
	 */
	public void removeChild(Folder aFolder) {
		this.children.remove(aFolder);
	}

	/**
	 * @return
	 */
	public List<? extends Folder> getFullTreeAsList() {
		List<Folder> options = new ArrayList<Folder>();
		for (Folder aFolder : this.getChildren()) {
			options.add(aFolder);
			System.out.println(aFolder.getFullTreeAsList());
			options.addAll(aFolder.getFullTreeAsList());
		}
		return options;
	}
	
	/**
	 * @param anAccess
	 */
	public void addAccess(Access anAccess){
		this.access.add(anAccess);
	}
	
	/**
	 * @param anAccess
	 */
	public void removeAccess(Access anAccess){
		this.access.remove(anAccess);
	}
	
	/**
	 * @return
	 */
	public Integer getNoOfAccesses() {
		return this.getAccesses().size();
	}
	
	public void addUserAccess(){
		
	}
	
	//FIXME: ME parece poco feliz que quede aca!
	public Folder createNew() {
		return this;
	}
	
	public Folder createNewUserAccess(){
		return this;
	}
	
	public Folder createNewGroupAccess(){
		return this;
	}
	
}